<template>
  <div class="home">
    <my-header
      @left-click="leftClick"
      @right-click="isShowCover = true"
    ></my-header>
    <div class="count">
      <my-cart
        v-for="(item, index) in goods"
        :key="index"
        :itemObj="item"
        @change="checkOne"
      ></my-cart>
    </div>
    <div class="foot">
      <label
        ><input
          type="checkbox"
          v-model="allCheck"
          @change="checkBtn"
        />全选</label
      >
      <span>总价：{{ allPrice }}</span>
      <span @click="del">{{ isPay ? "去支付" : "删除" }}{{ allNum }}</span>
    </div>
  </div>
  <transition name="slide-fade">
     <div class="cover" v-show="isShowCover">
      <h6>关键字搜索</h6>
      <ul>
        <li
          v-for="(item, index) in searchList"
          :key="index"
          :class="{ active: searchIndex == index }"
          @click="checkSearch(index)"
        >
          {{ item }}
        </li>
      </ul>
      <div class="foot">
        <button @click="isShowCover = false">取消</button>
        <button style="background: green; color: #fff" @click="save">
          确定
        </button>
      </div>
    </div>
  </transition>
    
</template>

<script>
import MyCart from "../components/myCart.vue";
import MyHeader from "../components/myHeader.vue";
// @ is an alias to /src
import axios from "axios";
import { reactive, toRefs, computed } from "vue";
export default {
  name: "Home",
  components: {
    MyHeader,
    MyCart,
  },
  setup() {
    const data = reactive({
      allList: [], // 存储原始数据
      goods: [], // 商品
      allCheck: false, // 全选的变量
      allPrice: computed(() => {
        // 选中的总价
        let price = 0;
        data.goods.forEach((item) => {
          if (item.checked) {
            price += item.num * item.price;
          }
        });
        return price;
      }),
      allNum: computed(() => {
        // 选中的总数
        let num = 0;
        data.goods.forEach((item) => {
          if (item.checked) {
            num += item.num;
          }
        });
        return num;
      }),
      isPay: true, // 是否去支付
      searchList: ["海信", "海尔", "山水", "美的"],
      searchIndex: null, // 选中的索引
      isShowCover: false, // 控制筛选条件是否显示
    });
    axios.get("data.json").then((res) => {
      data.allList = res.data.cartData;
      data.goods = res.data.cartData;
    });
    const checkBtn = () => {
      // 全选的函数
      data.goods.forEach((item) => {
        item.checked = data.allCheck;
      });
    };
    const checkOne = () => {
      // 单选
      // data.allCheck = data.goods.length?data.goods.every(item => item.checked):false;
      data.allCheck = data.goods.every((item) => item.checked);
    };
    const leftClick = (bool) => {
      data.isPay = bool;
    };

    const del = () => {
      // 删除的逻辑
      if (data.isPay) return;
      data.goods = data.goods.filter((item) => item.checked == false);
      data.allCheck = false;
    };

    const checkSearch = (i) => {
      // 筛选条件的选中

      if (data.searchIndex == i) {
        data.searchIndex = null;
      } else {
        data.searchIndex = i;
      }
    };
    const save = () => {
      // 搜索的确定事件
      let txt = data.searchList[data.searchIndex] || "";
      data.goods = data.allList.filter((item) => item.name.includes(txt));
      data.isShowCover = false;
    };
    return {
      ...toRefs(data),
      checkBtn,
      checkOne,
      leftClick,
      del,
      checkSearch,
      save,
    };
  },
};
</script>
<style lang="scss" scoped>
.home {
  background: #e9e9e9;
  height: 100%;
  .count {
    height: calc(100% - 100px);
    overflow: auto;
  }
}
.foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  background: rgba(206, 63, 6, 0.883);
  height: 50px;
  color: #fff;
}
.cover {
  position: fixed;
  width: 80%;
  background: #e9e9e9;
  height: 100%;
  top: 0;
  right: 0;
  z-index: 99;
  h6 {
    line-height: 50px;
    margin-left: 10px;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 20px;
    li {
      width: 60px;
      line-height: 26px;
      text-align: center;
      border-radius: 5px;
    }
    .active {
      border: 1px solid red;
    }
  }
  .foot {
    display: flex;
    padding: 0 20px;
    height: 50px;
    align-items: center;
    justify-content: space-between;
    background: transparent;
    button {
      border: none;
      padding: 5px 12px;
      border-radius: 5px;
      background: #fff;
    }
  }
}
.slide-fade-enter-active {
  transition: all 0.3s ease-out;
}

.slide-fade-leave-active {
  transition: all 0.3s ease-out;
}

.slide-fade-enter-from,
.slide-fade-leave-to {
  transform: translateX(100%);
}
</style>
